package com.wander.nio.chat.udp;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.nio.charset.StandardCharsets;

public class MyUdpServer {
    public static void main(String[] args) {
        try {
            DatagramSocket server = new DatagramSocket(9999);
            //循环读取消息
            while (true) {
                byte[] buffer = new byte[1024];
                DatagramPacket packet = new DatagramPacket(buffer, 1024);
                server.receive(packet);
                String message = new String(packet.getData(),0,packet.getLength(), StandardCharsets.UTF_8);
                System.out.println("接收消息：" + message);
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
